MCBank = function (string) {
	"use strict";
	var seed = {};
	seed.numQuestions = "";
	seed.name = [];
	seed.qtexts = [];
	seed.atexts = [];
	seed.feedback = [];
	seed.type = "mc-bank";
	
	seed.id = getUniqueId();
	var sidebar = $('body').find('.side-bar .middle'),
		preview = $('body').find('.preview'),
		doc = $(document);
	
	seed.genXML = function () {
		var self = this;
		var bank = "";
		var remainder = self.numQuestoins%self.qtexts.length;
		var num_per = Math.floor(self.numQuestions/self.qtexts.length);
				
		for(var i = 0; i < self.qtexts.length; i++)
		{
			var ansTemp = [];
			var added = 0;
			for(var j = 0; j < self.atexts.length ;j++)
			{
				if(j!=i)
				{
					ansTemp[added] = self.atexts[j];
					added++;
				}
			}
			var answers = [];
			if(remainder > 0) {
				answers = randomAnswers(ansTemp, self.numQuestions + 1);
				//console.log(num_per, 'added one');
				remainder--;
			} else {
				//console.log(num_per);
				answers = randomAnswers(ansTemp, self.numQuestions);
			}
			var correctNum = 3;
			for(var k = 0; k < answers.length; k++)
			{
				answers[k].push(self.atexts[i]);
			}
			//var name = "Question " + i;
			$.each(answers, function (index, element) {
				element.push(self.atexts[i]);
				bank += writeMCQuestion(self.name[i], self.qtexts[i], element, self.feedback[i], correctNum);
			});	
		}
		return bank;
	};
	
	seed.genGift = function () {
		var self = this;
		var giftString = "";
		var remainder = self.numQuestoins%self.qtexts.length;
		var num_per = Math.floor(self.numQuestions/self.qtexts.length);
		
		for(var i = 0; i < self.qtexts.length; i++)
		{
			var ansTemp = [];
			var added = 0;
			for(var j = 0; j < self.atexts.length ;j++)
			{
				if(j!=i)
				{
					ansTemp[added] = self.atexts[j];
					added++;
				}
			}
			var answers = [];
			if(remainder > 0) {
				answers = randomAnswers(ansTemp, self.numQuestions + 1);
				//console.log(num_per, 'added one');
				remainder--;
			} else {
				//console.log(num_per);
				answers = randomAnswers(ansTemp, self.numQuestions);
			}
			var correctNum = 3;
			$.each(answers, function (index, element) {
				element.push(self.atexts[i]);
				giftString += writeMCQuestionGIFT(self.name, self.qtexts[i], element, self.feedback[i], correctNum);
			});	
		}
		
		return giftString + "\n\n";
	};
	
	seed.genQSD = function () {
		var self = this;
		var qsdString = "";
		
		qsdString += "MCBANK:\r\n";
		qsdString += "" + self.numQuestions + "\r\n";
		for(var i = 0; i < self.qtexts.length; i++) 
		{
			qsdString += "" + self.name[i] + "; " + self.qtexts[i] + "; " + self.atexts[i] + "; " + self.feedback[i] + "\r\n";
		}
		return qsdString;
	};
	
	seed.set = function (options) {
		var self = this;
		$.each(options, function (key, value) {
			if (key === 'name') {
				self.name = value;
			} else if (key === 'qtexts') {
				self.qtexts = value;
			} else if (key === 'feedback') {
				self.feedback = value;
			} else if (key === 'numQuestions') {
				self.numQuestions = value;
			} else if (key === 'atexts') {
				self.atexts = value;
			}
		});
	};
	
	var randomAnswers = function (ratext/*[]*/, max)
	{
			var len = ratext.length;
			var retVal = [];			
			var n = 0;
			
			//Three for loops (choose three)
			for (var i = 0; i < len - 2; i++)
			{
				for (var j = i + 1; j < len - 1; j++)
				{
					for (var k = j + 1; k < len; k++) {
						retVal.push([ratext[i], ratext[j], ratext[k]])
						n++;
						if(n >= max) {
							return retVal;
						}
					}
				}
			}
			//console.log(retVal);
			return retVal;
	};
	
	var writeMCQuestion = function (name, qtext, watext/*[]*/, feedback/*[]*/, correctNum)
	{
		var string = "";
		//write category
		//string += "\t<question type=\"category\">\n\t\t<category><text>$course$/" + self.category + "</text>\n\t</category>\n</question>";		
		//write MC header
		string += "\t<question type =\"multichoice\">\n";
		//write Name
		string += "\t\t<name>\n\t\t\t<text> <![CDATA[<p>" + name + "</p>]]></text>\n\t\t</name>\n";
		//write Text
		string += "\t\t<questiontext format =\"html\">\n\t\t";
		string += "<text> <![CDATA[<p>" + qtext + "</p>]]></text>\n\t\t</questiontext>\n";
		//write Answer
		for(var i = 0; i < 4; i++)
		{
			if(i == correctNum)
			{
				string += writeMCAnswer(true, watext[i], feedback);
			}
			else
			{
				string += writeMCAnswer(false, watext[i], feedback);
			}
		}
		//write footer
		string += "\t\t<shuffleanswers>1</shuffleanswers>\n\t\t<single>true</single>\n\t\t<answernumbering>ABCD</answernumbering>\n\t</question>\n";
		return string;
	};

	var writeMCAnswer = function (correct, text, feedback)
	{
		var answer = "";
		answer += "\t\t<answer fraction=\"";
		if(correct)
		{	
			answer += "100";
		}
		else
		{
			answer += "0";
		}
		
		answer += "\">\n\t\t\t\t<text> <![CDATA[<p>" + text + "</p>]]></text>\n\t\t\t\t<feedback><text>" + feedback + "</text></feedback>\n\t\t</answer>\n";
		return answer;
	};
	
	var writeMCQuestionGIFT = function (name, qtext, watext/*[]*/, feedback/*[]*/, correctNum)
	{
		var string = "";
		//write categories
		string = "$CATEGORY: " + self.category;
		//write question
		//write Name
		string += "::" + self.name;
		//write Text
		string += "::" + self.qtext + "{";
		//write Answer
		for(var i = 0; i < 4; i++)
		{
			if(i == correctNum)
			{
				string += writeMCAnswerGIFT(true, watext[i], feedback);
			}
			else
			{
				string += writeMCAnswerGIFT(false, watext[i], feedback);
			}
		}
		//write footer
		string += "}\n\n";
		return string;
	};

	var writeMCAnswerGIFT = function (correct, text, feedback)
	{
		var answer = "";
		if(correct)
		{	
			answer += "=";
		}
		else
		{
			answer += "~";
		}
		
		answer += "" + text + "#" + feedback + " ";
		
		return answer;
	};
	
	seed.validate = function () {
		var self = this,
			error = "";
		// name is required
		for (var index = self.name.length - 1; index >= 0; index--) {
			if (self.isParticleEmpty(index)) {
				self.deleteParticle(index);
			} else {
				if(self.name[index] === "") {
					if(error !== "") {
						error += ", ";
					}
					error += "Particle " + (index + 1) + " needs a name";
				}
				if(self.qtexts[index] === "") {
					if(error !== "") {
						error += ", ";
					}
					error += "Particle " + (index + 1) + " needs a question text";
				}
				if(self.atexts[index] === "") {
					if(error !== "") {
						error += ", ";
					}
					error += "Particle " + (index + 1) + " needs an answer text";
				}
			}
		}
		
		if (self.name.length < 4) {
			if(error !== "") {
				error += ", ";
			}
			error += "There must be at least four question/answer pairs"
		}
		
		return error;
	};
	
	seed.deleteParticle = function (index) {
		var self = this;
		
		self.name.splice(index, 1);
		self.atexts.splice(index, 1);
		self.qtexts.splice(index, 1);
		self.feedback.splice(index, 1);
	};
	
	seed.isParticleEmpty = function (index) {
		var self = this;
		var t1 = (self.name[index] === "" && self.atexts[index] === "" && self.qtexts[index] === "" && self.feedback[index] === "");
		var t2 = (self.name[index] === undefined && self.atexts[index] === undefined && self.qtexts[index] === undefined && self.feedback[index] === undefined);
		return (t1 || t2);
	};
	
	if (string) {
		// spilt the string up by line
		var self = seed,
			lines = string.split("\r\n"),
			currentLine = 2;
			
		if (lines.length === 1) {
			lines = string.split("\n");
		}
		
		// set up the options object
		var obj = {};
		obj.numQuestions = lines[1].trim();
		obj.name = [];
		obj.qtexts = [];
		obj.atexts = [];
		obj.feedback = [];
		obj.category = [];
		
		// go through the lines with array data
		// trim is to get rid of leading and trailing whitespace
		while (currentLine < lines.length) {
			var line = lines[currentLine];
			// javascript does not support negative lookbehind so reverse \;
			// so negative lookahead can be used
			line = line.replace("\\;", ";\\");
			// split the line into its parts (name, qtext, atext, feedback)
			var data = line.split(/;(?!\\)/);
			$.each(data, function(index) {
				data[index] = data[index].replace(";\\", "\\;");
			});
			if (!(data.length === 1 && data[0] === "")) {
				obj.name.push(data[0].trim());
				obj.qtexts.push(data[1].trim());
				obj.atexts.push(data[2].trim());
				obj.feedback.push(data[3].trim());
			}
			currentLine++;
		}
		
		seed.set(obj);
	};
	
	return seed;
};
